package com.fingard.dsp.bank.directbank.ynht01;

import java.util.Date;

import com.fingard.constant.Format;
import com.fingard.dsp.bank.directbank.DirectBase;
import com.fingard.net.WebRequest;
import com.fingard.text.StringHelper;
import com.fingard.xml.XmlTextWriter;

/**
 * Created by F on 2019/3/12.
 */
public class YNHT01Base extends DirectBase{

    protected void writeCommonHeader(XmlTextWriter xmlWriter, YNHT01Header p_bankHeader) throws Exception {
        if (StringHelper.isNullOrEmpty(p_bankHeader.tranCode)) {
            throw new Exception("未赋值trnCode");
        }
        xmlWriter.writeStartDocByDeclaration("<?xml version=\"1.0\" encoding=\"" + getCharset() + "\"?>");
        xmlWriter.writeStartElement("HTBankData");
        xmlWriter.writeStartElement("ReqData");
        xmlWriter.writeStartElement("ReqHead");
        xmlWriter.writeElementString("Version",getBankConfig().getValueAsString("version"));
        xmlWriter.writeElementString("TranCode",p_bankHeader.tranCode);
        xmlWriter.writeElementString("TermDate",p_bankHeader.termDate);
        xmlWriter.writeElementString("TermTime",p_bankHeader.termTime);
        xmlWriter.writeElementString("SeqNo",p_bankHeader.seqNo);
        xmlWriter.writeElementString("CorpId",bankFront.org);
        xmlWriter.writeEndElement();
    }

    protected String getQryTransID(){
        Date tmpDateNow = new Date();
        return Format.Time12Format.format(tmpDateNow);
    }

    protected String getCharset() {
        return getBankConfig().getCharset("GBK");
    }

    protected String[] sendToBank(String p_reqStr) throws Exception {

        String[] retStr = new String[]{"", ""};

        WriteBankLogLn("2.提交银行的地址：" + bankFront.serverURL);
        WriteBankLogStep2(p_reqStr);
        //发送银行前置
        WebRequest tmpWebRequest = new WebRequest(bankFront.serverURL);
        tmpWebRequest.setConnectTimeout(60000);
        tmpWebRequest.setRequestProperty("Content-Type", "application/x-NS-BDES");
        tmpWebRequest.setRequestProperty("Connection", "Close");
        String charsetName = getCharset();
        byte[] tmpUpBytes = p_reqStr.getBytes(charsetName);
        retStr = tmpWebRequest.upload(tmpUpBytes, charsetName);

        WriteBankLogStep3(retStr[1]);
        return retStr;
    }

    protected YNHT01Header headerInit(String tranCode,String req_Id){
        YNHT01Header header = new YNHT01Header();
        header.tranCode =tranCode;
        Date date = new Date();
        header.termDate = Format.Date8Format.format(date);
        header.termTime = Format.Time6Format.format(date);
        header.seqNo = req_Id;
        return header;
    }
}
